/**
 * @purpose 批量插入数据到数据库服务的基础实现
 * @author daizs
 * @created Feb 9, 2010
 */
package platform.common.service.impl;

import org.hibernate.StatelessSession;

import platform.common.bo.IIdentifiableObj;
import platform.common.dao.IDomainProvider;
import platform.common.dao.impl.BatchOperateDaoImpl;
import platform.common.dao.impl.DaoConfigException;
import platform.common.dao.impl.DaoException;
import platform.common.exception.BusinessException;
import platform.common.service.IBatchAddService;



public class GeneralBatchAddServiceImpl implements IBatchAddService {
	/**
	 * 批量操作Dao。
	 */
	protected BatchOperateDaoImpl batchDao;
	/**
	 * 领域对象提供者。
	 */
	protected IDomainProvider domainProvider;


	/*
	 * @see platform.common.service.IBatchAddService#insertIntoDB(platform.common.bo.IdentifiableObj)
	 */
	public void insertIntoDB(IIdentifiableObj bo) throws BusinessException {
		StatelessSession session;
		try {
			session = batchDao.currentSession();
		} catch (DaoException e) {
			throw new BusinessException(e);
		}
		session.insert(bo);
	}


	/*
	 * @see platform.common.service.IBatchAddService#commitAndCloseSession()
	 */
	public void commitAndCloseSession() throws BusinessException {
		try {
			batchDao.closeCurrentSessionAndCommit();
		} catch (DaoException e) {
			throw new BusinessException(e);
		}
	}

	/*
	 * @see platform.common.service.IBatchAddService#newEmptyDomainObj()
	 */
	public Object newEmptyDomainObj()throws BusinessException{
		try {
			return domainProvider.newEmptyObj();
		} catch (DaoConfigException e) {
			throw new BusinessException(e);
		}
	}

	/*
	 * @see platform.common.service.IBatchAddService#rollbackAndCloseSession()
	 */
	public void rollbackAndCloseSession() throws BusinessException {
		try {
			batchDao.closeCurrentSessionAndRollback();
		} catch (DaoException e) {
			throw new BusinessException(e);
		}
	}

	public BatchOperateDaoImpl getBatchDao() {
		return batchDao;
	}

	public void setBatchDao(BatchOperateDaoImpl batchDao) {
		this.batchDao = batchDao;
	}

	public IDomainProvider getDomainProvider() {
		return domainProvider;
	}

	public void setDomainProvider(IDomainProvider domainProvider) {
		this.domainProvider = domainProvider;
	}



}
